package jp.fsoriented.toishi;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import jp.fsoriented.toishi.data.Result;
import jp.fsoriented.toishi.data.Verse;
import jp.fsoriented.toishi.util.L;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

/**
 * 履歴画面
 * 
 * @author LapisCactus
 * 
 */
public class HistoryActivity extends SherlockActivity {

	public final static String INTENT_PARAM_VERSE = "verse";

	private HistoryAdapter adapter;
	private Verse verse;

	private final SimpleDateFormat form = new SimpleDateFormat();

	/**
	 * HistoryActivityに遷移する
	 * 
	 * @param context
	 * @param verse 履歴を確認したいみことば
	 */
	public static final void start(Context context, Verse v) {
		Intent intent = new Intent();
		intent.setClass(context, HistoryActivity.class);
		intent.putExtra(INTENT_PARAM_VERSE, v);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		L.onCreate("History");
		setContentView(R.layout.activity_history);
		adapter = new HistoryAdapter(this, new ArrayList<Result>());
		ListView listview = (ListView) findViewById(R.id.activity_history_list);
		listview.setAdapter(adapter);
		// インテントからデータを取得
		Intent intent = getIntent();
		if (intent != null) {
			Parcelable parc = intent.getParcelableExtra(INTENT_PARAM_VERSE);
			if ((parc != null) && (parc instanceof Verse)) {
				verse = (Verse) parc;
			}
		}
		// ActionBar設定
		ActionBar abar = getSupportActionBar();
		abar.setDisplayHomeAsUpEnabled(true);// onOptionsItemSelectedでR.id.homeがイベントで通知されるようになる
	}

	@Override
	protected void onResume() {
		super.onResume();
		L.onResume("History");
		if (verse == null) {
			finish();
			return;
		}

		adapter.clear();
		List<Result> results = verse.result;
		for (Result r : results) {
			adapter.add(r);
		}
		String title = getResources().getString(R.string.history_actionbar_title) + " " + verse.okCount + "/"
				+ verse.result.size();
		setTitle(title);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		L.onDestroy("History");
	}

	/**
	 * 閉じるボタン
	 * 
	 * @param v
	 */
	public void onCloseClick(View v) {
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case android.R.id.home:
			L.d("Back selected.");
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * HistoryAdapterで使われるビュー（一行分）を保持する。
	 * 
	 * ビューにタグとして登録し、表示を高速化する。
	 */
	class HistoryHolder {

		public TextView date;
		public TextView result;

	}

	/**
	 * 履歴一覧の要素・ビュー
	 */
	class HistoryAdapter extends ArrayAdapter<Result> {

		private LayoutInflater inflater;
		private List<Result> list;

		/**
		 * コンストラクタ
		 * 
		 * @param context
		 * @param textViewResourceId
		 * @param objects
		 */
		public HistoryAdapter(Context context, List<Result> objects) {
			super(context, R.layout.activity_history_list_row, objects);
			list = objects;
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		/**
		 * ビューを作成する
		 */
		public View getView(final int position, View convertView, final ViewGroup parent) {
			HistoryHolder holder = null;
			// 新規行ビュー作成。または既存のビューを取得。
			View v = convertView;
			if (v == null) {
				v = inflater.inflate(R.layout.activity_history_list_row, null);
				holder = new HistoryHolder();
				holder.date = (TextView) v.findViewById(R.id.activity_history_list_text_date);
				holder.result = (TextView) v.findViewById(R.id.activity_history_list_text_result);
				v.setTag(holder);
			} else {
				holder = (HistoryHolder) v.getTag();
			}
			// 行データ適用
			if ((list != null) && (list.size() > position)) {
				Result item = list.get(position);
				holder.date.setText(form.format(item.date));
				holder.result.setText(item.ok ? "成功" : "失敗");
			}
			return v;
		}

	}

}
